import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:myturngame/bean/bagobject/bag_object.dart';
import 'package:myturngame/common/game_global.dart';

import '../../Theme.dart';
import '../../bean/bagobject/consume_object.dart';
import '../../bean/bagobject/equipment.dart';
import '../../bean/role.dart';
import '../text_decoration.dart';
import 'bag_object_info_dialog.dart';

///背包布局
class BagLayout extends StatefulWidget {
  const BagLayout({Key? key}) : super(key: key);

  @override
  State<BagLayout> createState() => _BagLayoutState();
}

class _BagLayoutState extends State<BagLayout> {
  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        Expanded(flex: 1, child: roleEquipment(GameGlobal.player)),
        const Divider(),
        Expanded(flex: 2, child: bagGrid(GameGlobal.player.bag.objectsInBag)),
      ],
    );
  }

  Widget bagGrid(List<BagObject> objectsInBag) {
    int gridCount = 5;
    return GridView.count(
      crossAxisCount: gridCount,
      crossAxisSpacing: 3,
      children: objectsInBag
          .map((e) => InkWell(
                onTap: () {
                  showDialog(
                      context: context,
                      builder: objectInfoDialogBuilder(e, (operation) {
                        _operationBagObject(operation, e);
                      }));
                },
                child: BorderContainer(
                  Text(
                    e.name,
                    style: TextStyle(
                        fontSize: 15.0 - e.name.length, color: e.color),
                  ),
                  alignment: Alignment.center,
                ),
              ))
          .toList(),
    );
  }

  Widget roleEquipment(Role player) {
    return Column(
      children: [
        Row(
          children: [
            const Text("头部："),
            itemLayout(player.equipHead),
          ],
        ),
        const Divider(color: Colors.transparent, height: 2),
        Row(
          children: [
            const Text("武器："),
            itemLayout(player.equipWeapon),
          ],
        ),
        const Divider(color: Colors.transparent, height: 2),
        Row(
          children: [
            const Text("身体："),
            itemLayout(player.equipBody),
          ],
        ),
        const Divider(color: Colors.transparent, height: 2),
        Row(
          children: [
            const Text("鞋子："),
            itemLayout(player.equipShoe),
          ],
        ),
      ],
    );
  }

  Widget itemLayout(Equipment? equipment) {
    if (equipment == null) {
      return BorderContainer(
        Text(equipment?.name ?? "无"),
        alignment: Alignment.center,
        width: 100,
      );
    } else {
      return GestureDetector(
        onTap: () {
          showDialog(
              context: context,
              builder: objectInfoDialogBuilder(equipment, (operation) {
                _operationBagObject(operation, equipment);
              }));
        },
        child: BorderContainer(
          Text(
            equipment.name,
            style: TextStyle(color: equipment.color),
          ),
          alignment: Alignment.center,
          width: 100,
        ),
      );
    }
  }

  ///操作物品
  void _operationBagObject(String operation, BagObject bagObject) {
    setState(() {
      if (operation == "装备" && bagObject is Equipment) {
        bagObject.equip(GameGlobal.player);
      } else if (operation == "卸下" && bagObject is Equipment) {
        bagObject.disEquip(GameGlobal.player);
      } else if (operation == "丢弃") {
        GameGlobal.player.bag.takeOut(bagObject);
      }
    });
  }
}
